package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.CommonAddresses;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.service.ICustomerService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.CommonReimbursements;
import com.ktgj.customer.service.ICommonReimbursementsService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 常用报销凭证信息Controller
 * 
 * @author ruoyi
 * @date 2021-09-01
 */
@ControllerAdvice
@RestController
@RequestMapping("/reimbursements")
public class CommonReimbursementsController extends BaseController
{
    @Autowired
    private ICommonReimbursementsService commonReimbursementsService;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private ICustomerService customerService;

    /**
     * 查询常用报销凭证信息列表
     */
    //@PreAuthorize(hasPermi = "customer:reimbursements:list")
    @GetMapping("/list")
    public TableDataInfo list(CommonReimbursements commonReimbursements)
    {
        startPage();
        List<CommonReimbursements> list = commonReimbursementsService.selectCommonReimbursementsList(commonReimbursements);
        return getDataTable(list);
    }

    /**
     * 导出常用报销凭证信息列表
     */
    @PreAuthorize(hasPermi = "customer:reimbursements:export")
    @Log(title = "常用报销凭证信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonReimbursements commonReimbursements) throws IOException
    {
        List<CommonReimbursements> list = commonReimbursementsService.selectCommonReimbursementsList(commonReimbursements);
        ExcelUtil<CommonReimbursements> util = new ExcelUtil<CommonReimbursements>(CommonReimbursements.class);
        util.exportExcel(response, list, "常用报销凭证信息数据");
    }

    /**
     * 获取常用报销凭证信息详细信息
     */
    //@PreAuthorize(hasPermi = "customer:reimbursements:query")
    @GetMapping(value = "/{commonReimbursementsId}")
    public AjaxResult getInfo(@PathVariable("commonReimbursementsId") Long commonReimbursementsId)
    {
        return AjaxResult.success(commonReimbursementsService.selectCommonReimbursementsById(commonReimbursementsId));
    }

    /**
     * 新增常用报销凭证信息
     */
    //@PreAuthorize(hasPermi = "customer:reimbursements:add")
//    @Log(title = "常用报销凭证信息", businessType = BusinessType.INSERT)
    @PostMapping("/addReimbursements")
    public AjaxResult addReimbursements(@RequestBody CommonReimbursements commonReimbursements)
    {
        return toAjax(commonReimbursementsService.insertCommonReimbursements(commonReimbursements));
    }

    /**
     * 修改常用报销凭证信息
     */
    //@PreAuthorize(hasPermi = "customer:reimbursements:edit")
    @Log(title = "常用报销凭证信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonReimbursements commonReimbursements)
    {
        return toAjax(commonReimbursementsService.updateCommonReimbursements(commonReimbursements));
    }

    /**
     * 删除常用报销凭证信息
     */
    //@PreAuthorize(hasPermi = "customer:reimbursements:remove")
    @Log(title = "常用报销凭证信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commonReimbursementsIds}")
    public AjaxResult remove(@PathVariable Long[] commonReimbursementsIds)
    {
        return toAjax(commonReimbursementsService.deleteCommonReimbursementsByIds(commonReimbursementsIds));
    }

    /**
     * 我的-新增常用报销凭证
     * */
    //@SignatureValidation
    @PostMapping("/editCommonlyUsedCertificate")
    public AjaxResult editCommonlyUsedCertificate(HttpServletRequest request, @RequestBody CommonReimbursements commonReimbursements){
        Long customerId;
        if (commonReimbursements.getCustomerId() != null && commonReimbursements.getCustomerId() != 0){
            customerId = customerService.selectCustomerById(commonReimbursements.getCustomerId()).getCustomerId();
        }
        else {
            try {
                customerId = loginService.getLoginCustomer(request).getCustomerId();
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
        }
        commonReimbursements.setCustomerId(customerId);
        return AjaxResult.success(commonReimbursementsService.saveCommonReimbursementsByCustomerId(commonReimbursements));
    }

    /**
     * 查询待编辑的报销凭证
     * */
    @PostMapping("/selectReimbursementsById")
    public AjaxResult certificateToBeModified(@RequestBody Map<String, Object> map){
        Long commonReimbursementsId = Long.valueOf(map.get("commonReimbursementsId").toString());
        CommonReimbursements commonReimbursements = commonReimbursementsService.certificateToBeModified(commonReimbursementsId);
        if (commonReimbursements != null){
            return AjaxResult.success(commonReimbursements);
        }
        else {
            return AjaxResult.error("未查询到数据");
        }
    }

    /**
     * 修改报销凭证
     * */
    @PostMapping("/updateReimbursements")
    public AjaxResult modifyTheCertificate(@RequestBody CommonReimbursements commonReimbursements){
        int byCustomerId = commonReimbursementsService.updateCommonReimbursementsByCustomerId(commonReimbursements);
        if (byCustomerId > 0){
            return toAjax(byCustomerId);
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 常用发票抬头
     * */
    @PostMapping("/selectReimbursements")
    public AjaxResult purchaseQueryCommon(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        List<CommonReimbursements> commonReimbursementsList = commonReimbursementsService.purchaseQueryCommon(customerId);
        if (commonReimbursementsList != null && commonReimbursementsList.size() > 0){
            return AjaxResult.success(commonReimbursementsList);
        }
        else {
            return AjaxResult.error("没有数据");
        }
    }

    /**
     * 查询会员发票抬头
     * */
    @PostMapping("/selectVoucher")
    public AjaxResult selectVoucher(@RequestBody Map<String, Object> map){
        Long customerId;
        if (map.get("customerId") != null && !map.get("customerId").equals("")){
            customerId = Long.valueOf(map.get("customerId").toString());
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }

        List<CommonReimbursements> commonReimbursementsList = commonReimbursementsService.purchaseQueryCommon(customerId);
        if (commonReimbursementsList != null && commonReimbursementsList.size() > 0){
            return AjaxResult.success(commonReimbursementsList);
        }
        else {
            return AjaxResult.error("没有数据");
        }
    }

    /**
     * 删除发票信息
     * */
    @PostMapping("/deleteReimbursements")
    public AjaxResult deleteCommonReimbursements(@RequestBody Map<String, Object> map) {
        Long commonReimbursementsId = Long.valueOf(map.get("commonReimbursementsId").toString());
        int i = commonReimbursementsService.deleteCommonReimbursements(commonReimbursementsId);
        if (i > 0){
            return AjaxResult.success("删除成功");
        }
        else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * id查信息
     * */
    @PostMapping("selectVoucherById")
    public AjaxResult selectCommonReimbursementsBycommonReimbursementsId(@RequestBody Map<String, Object> map) {
        if (map.get("commonReimbursementsId") != null && !map.get("commonReimbursementsId").equals("")){
            Long commonReimbursementsId = Long.valueOf(map.get("commonReimbursementsId").toString());
            CommonReimbursements commonReimbursements = commonReimbursementsService.selectCommonReimbursementsBycommonReimbursementsId(commonReimbursementsId);
            if (commonReimbursements != null){
                return AjaxResult.success(commonReimbursements);
            }
            else {
                return AjaxResult.error("没有数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
